package auctionhausen

import static org.junit.Assert.*
import grails.converters.JSON

import org.junit.*

class ListingServiceIntegrationTests {
	def jmsService
	def listingService
	static def Boolean queueMessageRecieved = Boolean.FALSE

	def testSendJMS() {
		//cache number of messages on the queue before
		def queueMessagesBefore = jmsService.browse("auctionhausen.ListingCompletionQueue").size()
		def c1 = new Customer(email:'misss@test.umn.edu', password:'123456')
		c1.save(flush:true)

		//create date 5 seconds from now
		def fiveSecondsFromNow = new Date(new Date().time + 1000)
		//create listings
		def l1 = new Listing(
				name:"Monkey",
				endDate: fiveSecondsFromNow,
				startingPrice:4.5F,
				description:'sjflkasjflkdsfjljdkfs!',
				seller:c1)
		l1.save(flush:true)
		listingService.listingExpired(l1)

		//retrieve messages currently on the queue
		List messages = jmsService.browse("auctionhausen.ListingCompletionQueue")
		
		//there should be at least one more message
		assertTrue queueMessagesBefore < messages.size()
		
		//verify that the JSON message of the listing is on the queue
		def l1JSON = (l1 as JSON).toString()
		assertNotNull messages.find {
			it == l1JSON
		}
	}
}
